import requests
import configparser
import time
import hashlib
import string
import random
import datetime

#md5加密函数
def md5(text):
    md5 = hashlib.md5()
    md5.update(text.encode())
    return md5.hexdigest()

#ds算法函数
def getDS():
    n = 'cx2y9z9a29tfqvr1qsq6c7yz99b5jsqt'
    i = str(int(time.time()))
    r = ''.join(random.sample(string.ascii_lowercase + string.digits, 6))
    c = md5("salt=" + n + "&t=" + i + "&r=" + r)
    return "{},{},{}".format(i, r, c)

#获取配置文件数据函数
def getsz(key,vale):
    con = configparser.ConfigParser()
    con.read('米游社签到/config.ini')
    return con.get(key,vale)

#论坛签到函数
def ltqd():
    cookie = getsz('user','ltcookie')
    if cookie == "":
        print('没有论坛cookie，跳过执行。')
        return '论坛签到结果: 没有配置论坛cookie'
    else:
        sl = 1
        while sl <= 10:
            url = 'https://api-takumi.mihoyo.com/apihub/sapi/signIn'
            data = '{"gids":"'+ str(sl) +'"}'
            zz = requests.post(url=url,data=data,headers=head('user','ltcookie','app_bb')).json()
            if zz['message'] == 'OK':
                print('签到结果：{}签到成功'.format(sl))
            else:
                print('签到结果：'+ str(sl) + str(zz['message']))
            sl = sl+1
        return '论坛签到结果：' + zz['message']

#米游社帖子点赞
def mysdz():
    cookie = getsz('user','ltcookie')
    if cookie == "":
        print('没有论坛cookie，跳过执行。')
        return '论坛签到结果: 没有配置论坛cookie'
    else:
        sl = 0
        daurl = "https://api-takumi.mihoyo.com/apihub/sapi/upvotePost"
        url = "https://api-takumi.mihoyo.com/post/api/getForumPostList?forum_id=26&is_good=false&is_hot=false&last_id=&page_size=20&sort_type=1"
        dzysj = requests.get(url=url).json()
        while sl < 5:
            post_id = dzysj['data']['list'][sl]['post']['post_id']
            data = '{"is_cancel":false,"post_id":"'+ post_id +'"}'
            dafh = requests.post(url=daurl,data=data,headers=head('user','ltcookie','app_bb')).json()
            sl = sl + 1
            print(dafh['message'])
        return dafh['message']

#head头函数
def head(key,vale,app_version):
    head = {
        'User-Agent':'Mozilla/5.0 (Linux; Android 7.0; Meizu S6 Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/65.0.3325.110 Mobile Safari/537.36 miHoYoBBS/2.2.0',
        'x-rpc-device_id':'2eee2fdb-0cc1-3f25-8e5c-0e2b06439cbd',
        'referer':'https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html?bbs_auth_required=true&act_id=e202009291139501&utm_source=bbs&utm_medium=mys&utm_campaign=icon',
        'x-rpc-app_version':getsz(key,app_version),
        'Host':'api-takumi.mihoyo.com',
        'x-rpc-client_type':'5',
        'Content-Type':'application/json;charset=UTF-8',
        'Accept':'application/json, text/plain, */*',
        'cookie':getsz(key,vale),
        'X-Requested-With':'com.mihoyo.hyperion',
        'ds':getDS()
    }
    return head

#获取今日奖励信息函数
def getjlxx(sj):
    url = 'https://api-takumi.mihoyo.com/event/bbs_sign_reward/home?act_id=e202009291139501'
    zz = requests.get(url=url,headers=head('user','llqcookie','app_bb')).json()
    sy = int(sj)
    return '今日奖励: ' + zz["data"]['awards'][sy]['name'] + ' x ' + str(zz["data"]['awards'][sy]['cnt'])

#获取游戏uid函数
def qsj():
    url = 'https://api-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_cn'
    zz = requests.get(url=url,headers=head('user','llqcookie','app_bb')).json()
    return zz['data']['list'][0]

#获取累计签到函数
def zsj(qsj):
    url = 'https://api-takumi.mihoyo.com/event/bbs_sign_reward/info?region={}&act_id={}&uid={}'.format(qsj['region'],'e202009291139501',qsj['game_uid'])
    zz = requests.get(url=url,headers=head('user','llqcookie','app_bb')).json()
    return zz['data']['total_sign_day']

#游戏每日签到函数
def yxqd(qsj):
    url = 'https://api-takumi.mihoyo.com/event/bbs_sign_reward/sign'
    data = '{"act_id":"e202009291139501","region":"'+ qsj['region'] +'","uid":"'+ qsj['game_uid'] +'"}'
    zz = requests.post(url=url,data=data,headers=head('user','llqcookie','app_bb')).json()
    if zz['message'] == 'OK':
        return '签到结果: 签到完成'
    else:
        return '签到结果: ' + zz['message']

#推送通知函数
def qqts(event,context):
    ztsj = random.randint(0,300)
    print('暂停{}秒继续执行'.format(ztsj))
    time.sleep(ztsj)
    yxsj = qsj()
    ljqdts = zsj(yxsj)
    data = {'msg':'旅行者: ' + yxsj['nickname'] + '\n' + '服务器: ' + yxsj['region_name']+'\n'+'uid:'+yxsj['game_uid'] +'\n' + getjlxx(ljqdts) +'\n'+'本月签到: ' +str(ljqdts)+'次'+'\n'+ yxqd(yxsj) +'\n'+ ltqd() +'\n' +"米游社帖子点赞： " + mysdz()}
    url = 'https://qmsg.zendee.cn/send/{}'.format(getsz('user','skey'))
    zz = requests.post(url=url,data=data).json()
    if zz['code'] == 0:
        print(zz['reason'])
        return zz
    else:
        print(zz['reason'])
        return zz